#include "dialog_sql.h"
#include "ui_dialog_sql.h"
#include "QDir"
#include "mysql.h"
#include "dialog_msbox.h"
Dialog_sql::Dialog_sql(test* da,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_sql)
{
    ui->setupUi(this);
    buffer=da;

    this->setWindowFlags(Qt::FramelessWindowHint);
    t_btn_no=ui->btn_no;
    connect(ui->btn_no,&QPushButton::clicked,[=]{done(ENM_no_BTN);});
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//列宽自适应

}

Dialog_sql::~Dialog_sql()
{
    delete ui;
}
int Dialog_sql::show(test* da, QWidget *parent)
{

    Dialog_sql box(da);
    return box.exec();
}

void Dialog_sql::on_load_clicked()
{

    if(ui->tableWidget->currentRow()>-1)
    {
        int row=ui->tableWidget->currentRow();
        QString test_id=ui->tableWidget->item(row,0)->text();
        if(mysql::query_test_all(test_id,buffer))done(ENM_yes_BTN);
    }

}

void Dialog_sql::on_btn_sql_clicked()
{
    QString test_id=ui->test_id->text();
    QString test_d1=ui->test_d1->text();
    QList<QStringList>sql_data=mysql::query_test_info(test_id,test_d1);

    int table_row=ui->tableWidget->rowCount();
    for(int i=0;i<table_row;i++)ui->tableWidget->removeRow(0);

    if(sql_data.count()>0)
    {
        int row=sql_data.count();
        int column=sql_data.at(0).count();
        qDebug()<<column;
        ui->tableWidget->setRowCount(row);
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<column-1;j++)
            {
                ui->tableWidget->setItem(i,j,new QTableWidgetItem(sql_data.at(i).at(j)));
                ui->tableWidget->item(i,j)->setToolTip(sql_data.at(i).at(column-1));
            }
        }
    }
}

void Dialog_sql::on_del_clicked()
{
    int row=ui->tableWidget->currentRow();
    QString test_id=ui->tableWidget->item(row,0)->text();

    int ret=Dialog_msbox::show(1,1,"删除","删除信息",QString("请注意，是否对试验样品:%1 数据进行删除？").arg(test_id));
    if(ret==1)
    {
       if(mysql::del_test_ID(test_id)) Dialog_msbox::show(0,0,"删除","删除信息","试验样品数据删除成功！");
       on_btn_sql_clicked();
    }

}
